<?php
// -------------------------------------------------------------
// Copyright DTLink, LLC 2005.
// by: Yermo Lamers
//
// This software is governed by the new BSD License. Please see the
// accompanying LICENSE.txt file.
// 
// For more information please see:
//    http://www.formvista.com/otherprojects/draglist.html

// -------------------------------------------------------------
// For purposes of demonstration print out the order if submit
// was pressed.
//
// We make use of the fact that arrays in PHP work like associative arrays,
// so the offset in the array can be used as a "mysql unique key field" (i.e.
// it does not need to be sequential). The value is the position in the list.
// On Submit the values fields are updated before being submitted back to the
// server.
//
// --------------------------------------------------------------------

include($ROOT_DIR.'mode/stats_calc2.inc');


 if ( @$_POST["cmd"] == "reorder" )
	{

	print( "New Ordering for the " . $_POST["list"] . " list is:" );
$i = 0;
	foreach ( $_POST["draglist_items"] as $name => $value ) {
		print( "Image <b>" . $name . "</b> was moved to <b>" . $value . "</b><br>\n" );
		print($_POST['drag_item'][$value][artist]);
	}
 }
?>
<?php
//$measure_time = date('Y-m-d H:i:s', mktime(date(H), date(i), date(s), date(m), date(d) - 10, date(Y)));
$StatStartTime = $StatStart;

//this neat little statement tells us, assuming correct spelling and proper input, approximately how many unique albums have been played this week; we'll need this to calculate frequency of each album's play.
$total_plays = mysql_fetch_array(mysql_query("SELECT COUNT(`album`) FROM `airplay` WHERE `timestamp`>'$StatStartTime' "));
$unique_album_plays = mysql_fetch_array(mysql_query("SELECT COUNT(DISTINCT LOWER(`album`)) FROM `airplay`  WHERE `timestamp`>'$StatStartTime' "));
$total_new_plays = mysql_fetch_array(mysql_query("SELECT COUNT(DISTINCT `cddbid`) FROM `airplay` WHERE `status`='2' AND `cddbid`!='' AND `timestamp`>'$StatStartTime' "));

// we have a special temporary table where we're going to store data we collect in the next statement
mysql_query("DELETE FROM `temp_stat` WHERE `id`>'-1'") or die(mysql_error()); // because this table is temporary, we should make sure it's tabula rasa when we use it.

$thirteen_weeks_ago = date('Y-m-d H:i:s', mktime(date(H), date(i), date(s), date(m)-3, date(d), date(Y)));

//this statement will grab all the CDs that we care about, new ones... we wont be charting anything else, obviously
$stat_query = "SELECT * FROM `airplay` WHERE  (`status`<'3') AND (`cddbid`!='') AND (`timestamp`>'$StatStartTime') AND (`add_date`>'$thirteen_weeks_ago') ";
$stat_result = mysql_query($stat_query);
while($stat_row = mysql_fetch_assoc($stat_result)) {
	$temp_query = "SELECT * FROM `temp_stat` WHERE `cdid`='".$stat_row[cddbid]."' LIMIT 1";
	$temp_result = mysql_query($temp_query) or die(mysql_error());	
	if ($temp_row = mysql_fetch_assoc($temp_result)) {
		$spins = $temp_row[spins];
		$spins++;
		mysql_query("UPDATE `temp_stat` SET `spins`='".$spins."' WHERE `cdid`='".$stat_row[cddbid]."' ") or die(mysql_error());
	} else {
		mysql_query("INSERT INTO `temp_stat` SET `spins`='1', `cdid`='".$stat_row[cddbid]."' ") or die(mysql_error());
	}
}
?>

<form name="draglist_form" action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="button" value="Export &gt;&gt;" onClick="javascript:draglist_manager.do_submit('draglist_form','draglist_container')">

<input type="hidden" name="mode" value="cmjexport" />
<input type="hidden" name="cmd" value="reorder" />
<input type="hidden" name="list" value="vertical" />
<table><tr><td>Rating</td><td>unique Frequency</td><td>total frequency</td><td>New Frequency</td><td>Rotation</td><td>Spins</td><td>Slot</td></tr><tr><td colspan="7">
<div id="draglist_container">
<?php
$rating = 0;
$statistics_query = "SELECT * FROM `temp_stat` ORDER BY `spins` DESC";
$statistics_result = mysql_query($statistics_query) or die(mysql_error());
while ($statistics_row = mysql_fetch_assoc($statistics_result)) {
?><div style="position: relative; left: 0px; top: 0px;">
<table><tr><td><span style="font-weight: bold;"><?=++$rating?>. </span></td><td><input type="text" name="drag_item[<?=$rating?>][artist]" value="<?=htmlspecialchars(@mysql_result(mysql_query("SELECT `artist` FROM `album` WHERE `cddbid`='".$statistics_row[cdid]."' "), 0)) ?>" /> <input type="text" name="drag_item[<?=$rating?>][album]" value="<?=@mysql_result(mysql_query("SELECT `title` FROM `album` WHERE `cddbid`='".$statistics_row[cdid]."' "), 0) ?>" /></i></td><td>(<?=$statistics_row[spins]?>) <?php
if ((($rating - 1) <= ($total_new_plays[0] / 3)) || (($statistics_row[spins]) == $heavy_spins)) {
	echo('Heavy');
	$heavy_spins = $statistics_row[spins];
} elseif ((($rating - 1) <= ($total_new_plays[0] / 1.5)) || (($statistics_row[spins]) == $medium_spins)) {
	echo('Medium');
	$medium_spins = $statistics_row[spins];
} else {
	echo('Light');
}
?></td><td><?php echo ((round(($statistics_row[spins] / $unique_album_plays[0]), 4) * 100) . '%') ?></td><td><?php echo ((round(($statistics_row[spins] / $total_plays[0]), 4) * 100) . '%') ?></td><td><?php echo ((round(($statistics_row[spins] / $total_new_plays[0]), 4) * 100) . '%') ?></td></tr></table><input name="draglist_items[<?=$rating?>]" value="<?=($rating - 1)?>" type="hidden"></div>
<?php
}
?></div></td></tr>
</table>
</form>



<script language="JavaScript">
// a bit ugly. draglist.js assumes the existence of a global
// dragListIndex array.
var dragListIndex = new Array();

// manager classes. 
draglist_manager = new fv_dragList( 'draglist_container' );

// queries all top level <divs> under the draglist_container
// div and sets up dragging.
draglist_manager.setup();

// addes the newly created dragList to the list of draglists on 
// the page (i.e. we can have more than one on a page)
addDragList( draglist_manager );
</script>
<form name="cmj_export"><a href="#" onclick="javascript:document.getElementById('cmj_output').select();" >Select All</a><br />
<textarea style="width: 600px; height: 466px;" id="cmj_output">
for () {
	
}
</textarea></form>
